package com.star.mall.filter;

import com.star.mall.handler.JwtTokenHandler;
import com.star.mall.model.UserDetail;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证拦截
 * @author StarQ
 * @since 2021-04-21
 */
@Service
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Resource
    JwtTokenHandler jwtTokenHandler;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        String authToken = request.getHeader("Authorization");
        String authTokenStart = "Bearer ";

        if (!StringUtils.isEmpty(authToken) && authToken.startsWith(authTokenStart)){
            authToken = authToken.substring(authTokenStart.length());
            String username = jwtTokenHandler.getUsernameByToken(authToken);

            if (!StringUtils.isEmpty(username) && SecurityContextHolder.getContext().getAuthentication() == null){
                UserDetail userDetail = jwtTokenHandler.getUserByToken(authToken);
                if (jwtTokenHandler.validateToken(authToken)){
                    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetail,null,userDetail.getAuthorities());
                    authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                }
            }
        }
        chain.doFilter(request, response);
    }
}
